<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" style="width:500px" ref="dialogForm" label-width="80px"
			label-position="right">
			<el-form-item label="单位名称" prop="name">
				<el-input v-model="FormData.name" clearable placeholder="请填写单位名称"></el-input>
			</el-form-item>
			<el-form-item label="单位简称" prop="asName">
				<el-input v-model="FormData.asName" clearable placeholder="请填写单位简称"></el-input>
			</el-form-item>
			<el-form-item label="联系人" prop="linkName" placeholder="请填写单位联系人">
				<el-input v-model="FormData.linkName" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系电话" prop="linkTel" placeholder="请填写联系电话">
				<el-input v-model="FormData.linkTel" clearable></el-input>
			</el-form-item>
			<el-form-item label="详细地址" prop="address" placeholder="请填写单位详细地址">
				<el-input v-model="FormData.address" clearable></el-input>
			</el-form-item>
			<el-form-item label="登录账号" prop="account">
				<el-input v-model="FormData.account" :disabled="FormData.id" placeholder="用于登录系统" clearable></el-input>
			</el-form-item>
			<el-form-item label="登录密码" :prop="FormData.id ? '':'password'">
				<el-input type="password" v-model="FormData.password" clearable show-password
					placeholder="密码不修改,请留空"></el-input>
			</el-form-item>
			<el-form-item label="所属角色" prop="">
				<sc-select v-model="FormData.roles" :apiObj="$API.system.role.list" :props="rolesProps" :params="rolesParams" clearable filterable
					multiple style="width: 100%;"></sc-select>
				<!-- <el-select v-model="FormData.roles" multiple filterable style="width: 100%">
					<el-option v-for="item in rolesList" :key="item.id" :label="item.name" :value="item.id" />
				</el-select> -->
			</el-form-item>
			<el-form-item label="激活状态" prop="action">
				<el-switch v-model="FormData.action"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				FormData: {
					name: '',
					action: false,
					password:''
				},
				rolesParams: {
					page: 1,
					pagesize: 200
				},
				rolesProps: {
					label: 'name',
					value: 'id'
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入单位名称'
					}],
					linkName: [{
						required: true,
						message: '请输入联系人'
					}],
					linkTel: [{
						required: true,
						message: '请输入联系电话'
					}],
					account: [{
						required: true,
						message: '请输入登录账号'
					}],
					password: [{
						required: true,
						message: '请输入登录密码'
					}, ],

				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				aeraList: [{
					value: 'guide',
					label: '海南省',
					children: [{
						value: 'disciplines',
						label: '海口市',
						children: [{
							value: 'consistency',
							label: '美兰区',
						}],
					}, ],
				}]
			}
		},
		watch: {
			'form.address'(nVal) {
				if (!nVal) {
					this.showAMap = false
				} else {
					this.showAMap = true
				}
			}
		},
		mounted() {
		},
		methods: {
			comfirmBmap(mapData) {
				console.log(mapData)
				this.form.lng = mapData.lng
				this.form.lat = mapData.lat
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true
				let roles = []
				if (this.FormData.roles.length) {
					this.FormData.roles.forEach(item => {
						roles.push({
							id: item
						})
					})
				} else {
					roles = null
				}
				let data = {
					"name": this.FormData.name,
					"user": {
						"account": this.FormData.account,
						"password": this.FormData.password,
						"action": this.FormData.action,
						"roles": roles,
					},
					"asName": this.FormData.asName,
					"linkName": this.FormData.linkName,
					"linkTel": this.FormData.linkTel,
					"address": this.FormData.address

				}
				if (this.FormData.id) {
					data.id = this.FormData.id
					data.user.id = this.FormData.userId
				}
				var res = await this.$API.enterprise.unit.save.post(data)
				this.isSaveing = false
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.FormData.id = data.id
				this.FormData.name = data.name
				this.FormData.asName = data.asName
				this.FormData.linkName = data.linkName
				this.FormData.linkTel = data.linkTel
				this.FormData.address = data.address
				this.FormData.account = data.userAccount
				this.FormData.userId = data.userId
				this.FormData.action = data.action
				this.FormData.roles = data.userRoles
				if (data.userRoles && data.userRoles.length) {
					let _arr = []
					this.FormData.roles.forEach(item => {
						_arr.push(item.id)
					})
					this.FormData.roles = _arr
				}
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.formData, data)
			}
		}
	}
</script>

<style>
</style>
